package com.ajk.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ajk.server.model.response.UserRes;
import com.ajk.server.service.UsersService;

/**
 * 
 * @author chenxuan
 *
 */
@Controller
public class ViewController {
	
	@Autowired
	private UsersService userService;

	@RequestMapping(value="/")
	public String index(){
		
		return "back/login";
	}
	
	@RequestMapping(value="/view/register")
	public String register(){
		
		return "user/register";
	}
	
	@RequestMapping(value="/view/login")
	public String login(HttpServletRequest request){
		
		return "user/login";
	}

	@RequestMapping(value="/view/profile")
	public String profile(HttpServletRequest request){
		Map<String, Object> map = (Map<String, Object>)request.getSession().getAttribute("user");
		if(null == map) {
			request.setAttribute("uri", "view/profile");
			return "user/login";
		}
		String userId = map.get("userId").toString();
		Integer id = Integer.valueOf(userId);
		UserRes userRes = userService.findUsersById(id);
		request.setAttribute("userProfile", userRes);
		return "user/profile";
	}
	
	@RequestMapping(value="/view/qrcode")
	public String qrcode(HttpServletRequest request){
		Map<String, Object> map = (Map<String, Object>)request.getSession().getAttribute("user");
		if(null == map) {
			request.setAttribute("uri", "view/qrcode");
			return "user/login";
		}
		String userId = map.get("userId").toString();
		Integer id = Integer.valueOf(userId);
		UserRes userRes = userService.findUsersById(id);
		request.setAttribute("userProfile", userRes);
		return "user/qrcode";
	}
	
	
}